//
//  QuestionStateView.m
//  Learn
//
//  Created by cjg.mini on 2024/4/21.
//  Copyright © 2024 迟建国. All rights reserved.
//

#import "QuestionStateView.h"

@interface QuestionStateView ()
@property (nonatomic , strong)UIImageView *stateIMView;
@property (nonatomic , strong)UIButton *nextBtn;
@property (nonatomic , strong)UILabel *timeL;

@property (nonatomic , strong)CAShapeLayer *ringLayer;
@property (nonatomic, strong) NSTimer *timer;
@property (nonatomic, assign) NSUInteger time;
@property (nonatomic, assign) NSUInteger count;

@property (nonatomic, assign) NSUInteger style;

@end

@implementation QuestionStateView

-(instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        [self setupUI];
    }
    return self;
}

-(void)drawRect:(CGRect)rect {
    [self.layer addSublayer:self.ringLayer];
    self.ringLayer.hidden = YES;
}

- (void)setupUI {
    
    UIImageView *stateIMView = [[UIImageView alloc] init];
    stateIMView.image = [UIImage imageNamed:@"icon_play"];
    [self addSubview:stateIMView];
    [stateIMView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self).offset(10);
        make.centerX.equalTo(self.mas_centerX);
        make.width.height.offset(52);
    }];
    self.stateIMView = stateIMView;
    
    UIButton *nextBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [nextBtn setTitle:@"下一步" forState:UIControlStateNormal];
    [nextBtn setTitleColor:rgba(12, 183, 108, 1) forState:UIControlStateNormal];
    nextBtn.titleLabel.font = [UIFont fontWithName:@"PingFangSC-Semibold" size:16];
    [nextBtn addTarget:self action:@selector(nextAction:) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:nextBtn];
    [nextBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self).offset(-20);
        make.left.equalTo(self.stateIMView.mas_right).offset(20);
        make.centerY.equalTo(stateIMView.mas_centerY);
        make.height.offset(50);
    }];
    self.nextBtn = nextBtn;
    
    UIView *bottomView = [[UIView alloc] init];
    bottomView.backgroundColor = rgba(12, 183, 108, 0.1);
    [self addSubview:bottomView];
    [bottomView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(bottomView.mas_centerX);
        make.right.left.equalTo(self).offset(0);
        make.top.equalTo(self.stateIMView.mas_bottom).offset(20);
        make.height.offset(30);
    }];

    UILabel *timeL = [[UILabel alloc] init];
    timeL.font = [UIFont fontWithName:@"PingFangSC-Semibold" size:12];
    timeL.textColor = rgba(51, 51, 51, 1);
    [bottomView addSubview:timeL];
    [timeL mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(bottomView.mas_centerX);
        make.centerY.equalTo(bottomView.mas_centerY);
    }];
    self.timeL = timeL;
}

- (void)nextAction:(UIButton *)sender {
    if (self.nextBlock) {
        self.nextBlock();
    }
    
}

- (void)setQuestionStyle:(NSUInteger)style time:(NSUInteger)time {
    self.time = time;
    self.count = time;
    self.style = style;
    if (style == 1) {
        self.stateIMView.image = [UIImage imageNamed:@"icon_play"];
    }
    if (style == 2) {
        self.stateIMView.image = [UIImage imageNamed:@"yuedu"];
    }
    if (style == 3) {
        self.stateIMView.image = [UIImage imageNamed:@"icon_play"];
    }
    [self startLoading];
}

- (void)startLoading {
    self.ringLayer.hidden = NO;
    self.ringLayer.strokeEnd = 0;

    if(self.timer){
        [self.timer invalidate];
        self.timer = nil;
    }
    self.timer = [NSTimer scheduledTimerWithTimeInterval:1 repeats:YES block:^(NSTimer * _Nonnull timer) {
//        [self addRing:(self.time-self.count)*1.0/(self.time)];
        self.count = self.count - 1;
        NSString *str = nil;
        if (self.style == 1) {
            str = @"正在播放原音";
        }
        if (self.style == 2) {
            str = @"正在读题";
        }
        if (self.style == 3) {
            str = @"答题中";
        }
        self.timeL.text = [NSString stringWithFormat:@"%@ 剩余%lus",str,(unsigned long)(self.count)];

        if(self.count <= 0){
            [self endLoading];
        }
    }];
    
}

- (void)endLoading {
    if(self.timer){
        [self.timer invalidate];
        self.timer = nil;
    }
    self.ringLayer.hidden = YES;
}

- (void)addRing:(CGFloat)value {
    self.ringLayer.strokeEnd = value;
}

- (CAShapeLayer *)ringLayer {
    if (_ringLayer == nil) {
        _ringLayer = [[CAShapeLayer alloc] init];
        _ringLayer.strokeColor = COLOR_TOPIC_SELECT.CGColor;
        CGRect frame = self.stateIMView.frame;
        _ringLayer.frame = frame;
        CGFloat w = frame.size.width;
        UIBezierPath * circleP = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(-4, -4, w+8, w+8)];
        _ringLayer.path           = circleP.CGPath;
        _ringLayer.lineWidth      = 2;
        _ringLayer.strokeStart    = 0;
        _ringLayer.strokeEnd      = 0;
        _ringLayer.fillColor      = [UIColor clearColor].CGColor;
        _ringLayer.fillRule       = kCAFillRuleEvenOdd;
    }
    return _ringLayer;
}

@end
